var mongoose = require('mongoose');
const creatPetUser = async (data) => await mongoose.model('masters').create(data);
const changeStatus = async (_id,params) => {
    return await mongoose.model('admins').updateOne({_id},params)};

const getPetUserById = async (_id) => await mongoose.model('masters').findOne({
    _id
});
//========================供应商================
const getsupolierById = async (data) => await mongoose.model('suppliers').findOne(data);
const addSupplier = async (data) => await mongoose.model('suppliers').create(data);
//======================供应商================
const getStores = async () => await mongoose.model('stores').find()
    .populate({
        path: 'storeId'
    })
    .exec()
const getAllSupplier = async () => await mongoose.model('suppliers').find()
    .populate({
        path: 'supplierId'
    })
    .exec()
const deletePetUser = async ({
    _id
}) => await mongoose.model('masters').deleteOne({
    _id
});

const getAllPetuser = async (data) => {
    let {
        page,
        limit,
        type,
        sort
    } = data
    const mastersModel = mongoose.model('masters');
    const result = {}
    if (type) {
        type = JSON.parse(type)
        result.title = await await mastersModel.find(type);
    } else {
        type = {}
        result.title = await mastersModel.find();
    };
    result.title = result.title.length
    result.items = await mastersModel
        .find(type)
        .skip((page - 1) * limit)
        .limit(limit)
        .sort({
            _id: sort
        })
        .exec()
    return result
}
const petuserUpdate = async (_id, params) => {
    return await mongoose.model('masters').updateOne({
        _id
    }, params)
}


const creatAdmin = async (data) => await mongoose.model('admins').create(data);
const creatStore = async (data) => await mongoose.model('stores').create(data);
const getAllAdmin = async (data) => {
    let {
        page,
        limit,
        type,
        sort
    } = data
    const adminsModel = mongoose.model('admins');
    const result = {}
    if (type) {
        type = JSON.parse(type)
        result.title = await await adminsModel.find(type);
    } else {
        type = {}
        result.title = await adminsModel.find();
    };
    result.title = result.title.length
    result.items = await adminsModel
        .find(type)
        .skip((page - 1) * limit)
        .limit(limit)
        .sort({
            _id: sort
        })
        .exec()
    return result
}
const getAdminById = async (_id) => await mongoose.model('admins').findOne({
    _id
});
const getDorById = async (_id) => await mongoose.model('stores').findOne({
    _id
});
const deleteAdmin = async ({
    _id
}) => await mongoose.model('admins').deleteOne({
    _id
});
const adminUpdate = async (_id, params) => {
    return await mongoose.model('admins').updateOne({
        _id
    }, params)
}

//门店
const getstoreById = async (data) => await mongoose.model('stores').findOne(data);
module.exports = {
    deletePetUser,
    creatPetUser,
    getAllPetuser,
    getPetUserById,
    petuserUpdate,
    getStores,
    changeStatus,
    creatAdmin,
    getAllAdmin,
    deleteAdmin,
    adminUpdate,
    getAdminById,
    creatStore,
    getDorById,
    getsupolierById,
    addSupplier,
    getAllSupplier,
    getstoreById
}